<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="108px">
      <!-- 异常原因:1-来料异常,2-设备异常,3-VCU加工,4-IGBT加工,5-其他 -->
      <el-form-item label="异常原因" prop="expReason">
        <el-select v-model="queryParams.expReason" placeholder="请选择" clearable>
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="填报起止时间" prop="expTime">
        <el-date-picker v-model="queryParams.expTime" value-format="yyyy-MM-dd" type="daterange" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['nonworkorder:record:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['nonworkorder:record:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['nonworkorder:record:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['nonworkorder:record:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日期" align="center" prop="expTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="异常工时" align="center" prop="abnormalWorkinghours" />
      <el-table-column label="异常原因" align="center" prop="expReasonName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="填报人" align="center" prop="createBy" />
      <el-table-column label="填报时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['nonworkorder:record:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['nonworkorder:record:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改非工单异常工时填报对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="日期" prop="expTime">
          <el-date-picker clearable style="width:100%;" v-model="form.expTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="异常工时(分)" prop="abnormalWorkinghours">
          <el-input v-model="form.abnormalWorkinghours" placeholder="请输入异常工时(单位:分钟)" type="number" @input="validatePositiveInteger" @blur="validateBlur" />
        </el-form-item>
        <el-form-item label="异常原因" prop="expReason">
          <el-select v-model="form.expReason" placeholder="请选择异常原因" style="width:100%;">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getList, addRecord, updateRecord, delRecord } from "@/api/mes/nonworkorder/record";
import JsBarcode from 'jsbarcode';
export default {
  name: "NonworkorderRecord",
  dicts: ['sys_yes_no'],
  data() {
    return {
      //  异常原因:1-来料异常,2-设备异常,3-VCU加工,4-IGBT加工,5-其他
      options: [
        {
          value: 1,
          label: '来料异常'
        },
        {
          value: 2,
          label: '设备异常'
        },
        {
          value: 3,
          label: 'VCU加工'
        },
        {
          value: 4,
          label: 'IGBT加工'
        },
        {
          value: 5,
          label: '其他'
        },
      ],
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      slectRow: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 非工单异常工时填报表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        expTime: null,
        expReason: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        expTime: [
          { required: true, message: "日期不能为空", trigger: "change" }
        ],
        abnormalWorkinghours: [
          { required: true, message: "异常工时不能为空", trigger: "blur" }
        ],
        expReason: [
          { required: true, message: "异常原因不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 限制只能输入正整数
    validatePositiveInteger() {
      this.form.abnormalWorkinghours = this.form.abnormalWorkinghours.replace(/[^\d]/g, ''); // 只允许输入数字
    },
    // 失焦时，确保输入有效
    validateBlur() {
      if (!this.form.abnormalWorkinghours || Number(this.form.abnormalWorkinghours) <= 0) {
        this.form.abnormalWorkinghours = ''; // 为空或 ≤0 时清空
      }
    },
    /** 查询非工单异常工时填报列表 */
    getList() {
      this.loading = true;
      let param = {
        expReason: this.queryParams.expReason,
      }
      if (this.queryParams.expTime) {
        param.beginCreateTime = this.queryParams.expTime[0],
          param.endCreateTime = this.queryParams.expTime[1]
      }
      getList(param).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(err => {
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        expTime: null,
        abnormalWorkinghours: null,
        expReason: null,
        remark: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      this.slectRow = selection[0]
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加非工单异常工时填报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true
      this.title = "修改非工单异常工时填报"
      if (row && row.id) {
        this.form = JSON.parse(JSON.stringify(row))
      } else {
        this.form = JSON.parse(JSON.stringify(this.slectRow))
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids.join(',');
      this.$modal.confirm('是否确认删除非工单异常工时填报编号为"' + ids + '"的数据项？').then(function () {
        return delRecord(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      let param = {
        expReason: this.queryParams.expReason,
      }
      if (this.queryParams.expTime) {
        param.beginCreateTime = this.queryParams.expTime[0],
          param.endCreateTime = this.queryParams.expTime[1]
      }
      this.download('/mes/nonworkorder/record/export', {
        ...param
      }, `非工单工时填报.xlsx`)
    }
  }
};
</script>
